define(['angular','jquery','amazeui','echarts','datetimepicker'],function(angular,jquery,amazeui,echarts){//引入位置一定要对应上
    var directiveModule = angular.module('directiveModule', ['datetimepickerM','eChartsM']);
    /**
     * amazeUI 时间控件
     */
    angular.module('datetimepickerM', [])
     .directive("datetimepicker",function(){
         return {
             restrict : 'AE',//匹配模式，A：属性，E：elment 元素,M：注释方式，C：样式
             templateUrl : 'app/directives/tpl/datetimepicker.html',
             replace :true,
             scope:{
            	 attr:'@'
             },
             controller:'DatetimepickerCtrl'
         }
     }) 
     .controller('DatetimepickerCtrl',['$scope','$element',function(scope,element){
    	 //中文化
    	 $.fn.datetimepicker.dates['zh-CN'] = {
		  days: ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"],
		  daysShort: ["周日", "周一", "周二", "周三", "周四", "周五", "周六", "周日"],
		  daysMin:  ["日", "一", "二", "三", "四", "五", "六", "日"],
		  months: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
		  monthsShort: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
		  today: "今天",
		  suffix: [],
		  meridiem: ["上午", "下午"],
		  rtl: false // 从右向左书写的语言你可以使用 rtl: true 来设置
		};
    	 /**
    	  * 合并两个object对象
    	  * override 是否覆盖
    	  */
    	 var extend=function(o,n,override){ 
    		for(var p in n)
    			if(n.hasOwnProperty(p) && (!o.hasOwnProperty(p) || override))
    				o[p]=n[p]; 
    	 }; 
    	 var baseAttr = {//基础属性设置
			 language:  'zh-CN',
    		 autoclose: true,
    		 todayBtn:true,
    		 todayHighlight:true,
    		 minView:'2',
    		 pickerPosition:'bottom-left'
    	 };
    	 
    	 //调用 datetimepicker
    	 if(scope.attr==undefined || scope.attr==""){
    		 element.datetimepicker(baseAttr);
    	 }else{
    		 var dateAttr = angular.fromJson(scope.attr);
    		 extend(dateAttr,baseAttr); 
        	 element.datetimepicker(dateAttr);
    	 }
     }]);

    /**
     * 百度 echarts
     */
    angular.module('eChartsM', [])
    .directive('eCharts', function($http, $window) {
        function link($scope, element, attrs, ctrl) {
            var myChart = echarts.init(element[0]);
            attrs.$observe('eData', function() {//通过$observe监听attrs中绑定的option属性，可以通过ajax请求数据，动态更新图表。
                var option = $scope.$eval(attrs.eData);
                if (angular.isObject(option)) {
                    myChart.setOption(option);
                }
            }, true);
            
            $scope.getDom = function() {
                return {
                    'height': element[0].offsetHeight,
                    'width': element[0].offsetWidth
                };
            };
            // console.log($scope.getDom());
            $scope.$watch($scope.getDom(), function() {
                // resize echarts图表
                myChart.resize();
            }, true);
        }
        return {
            restrict: 'AE',
            link: link
        };
    });
});

